#include "CTC.h"
#include "stdlib.h"

uint8_t ackMessage[ACK_MESSAGE_LENGTH] = {ACKNOWLEDGE,0,0};

RCR_t* createNewRCR(linkID_t linkID, zone_t zone, sleepTime_t sleepTime, battLife_t battLife)
{
	RCR_t* newRCR = (RCR_t*)malloc(sizeof(RCR_t));
	if(newRCR != NULL)
	{
		newRCR -> linkID 			= linkID;
		newRCR -> zone 				= zone;
		newRCR -> ventStatus 	= OPEN;
		newRCR -> sleepTime 	= sleepTime;
		newRCR -> battLife	 	= battLife;
	}
	return newRCR;
}

RCR_t** deleteRCR(RCR_t** RCRarray, uint8_t index)
{
	if(RCRarray[index] != NULL)
	{
		free(RCRarray[index]);
		return realloc(RCRarray, sizeof(RCRarray) - sizeof(RCR_t*));
	}
	return NULL;
}

RCR_t** addNewRCR(RCR_t** RCRarray, uint8_t numRCRs, RCR_t* newRCR)
{
	RCR_t** newRCRarray;
	if(newRCR != NULL)
	{
		newRCRarray = realloc(RCRarray, (numRCRs + 1)*sizeof(newRCR));
		if(newRCRarray != NULL)
			newRCRarray[numRCRs] = newRCR;
	}
	return newRCRarray;
}

RTS_t* createNewRTS(linkID_t linkID, zone_t zone, sleepTime_t sleepTime, battLife_t battLife)
{
	RTS_t* newRTS = (RTS_t*)malloc(sizeof(RTS_t));
	if(newRTS != NULL)
	{
		newRTS -> linkID 			= linkID;
		newRTS -> zone 				= zone;
		// TODO: set initial temperature
		newRTS -> temp			 	= 0;
		newRTS -> sleepTime 	= sleepTime;
		newRTS -> battLife	 	= battLife;
	}
	return newRTS;
}

RTS_t** deleteRTS(RTS_t** RTSarray, uint8_t index)
{
	if(RTSarray[index] != NULL)
	{
		free(RTSarray[index]);
		return realloc(RTSarray, sizeof(RTSarray) - sizeof(RTS_t*));
	}
	return NULL;
}	

RTS_t** addNewRTS(RTS_t** RTSarray, uint8_t numRTSs, RTS_t* newRTS)
{
	RTS_t** newRTSarray = NULL;
	if(newRTS != NULL)
	{
		newRTSarray = realloc(RTSarray, (numRTSs + 1)*sizeof(newRTS));
		if(newRTSarray != NULL)
			newRTSarray[numRTSs] = newRTS;
	}
	return newRTSarray;
}

uint8_t isRCR(RCR_t** RCRarray, uint8_t numRCRs, linkID_t linkID)
{
	uint8_t i;
	for(i = 0; i < numRCRs; i++)
		if(RCRarray[i] -> linkID == linkID)
			return 1;
	return 0;
}

uint8_t isRTS(RTS_t** RTSarray, uint8_t numRTSs, linkID_t linkID)
{
	uint8_t i;
	for(i = 0; i < numRTSs; i++)
		if(RTSarray[i] -> linkID == linkID)
			return 1;
	return 0;
}

uint8_t findRCRbyLinkID(RCR_t** RCRarray, uint8_t numRCRs, linkID_t linkID)
{
	uint8_t i;
	for(i = 0; i < numRCRs; i++)
		if(RCRarray[i] -> linkID == linkID)
			return i;
	return NO_DEVICE_FOUND;
}

uint8_t findRTSbyLinkID(RTS_t** RTSarray, uint8_t numRTSs, linkID_t linkID)
{
	uint8_t i;
	for(i = 0; i < numRTSs; i++)
		if(RTSarray[i] -> linkID == linkID)
			return i;
	return NO_DEVICE_FOUND;
}

void updateRCR(RCR_t** RCRarray, uint8_t numRCRs, linkID_t linkID, battLife_t battLife)
{
	uint8_t index = findRCRbyLinkID(RCRarray, numRCRs, linkID);
	RCRarray[index] -> battLife = battLife;
}

void updateRTS(RTS_t** RTSarray, uint8_t numRTSs, linkID_t linkID, temp_t temp, battLife_t battLife)
{
	uint8_t index = findRTSbyLinkID(RTSarray, numRTSs, linkID);
	RTSarray[index] -> temp = temp;
	RTSarray[index] -> battLife = battLife;
}

void ackRCR(RCR_t** RCRarray, uint8_t numRCRs, linkID_t linkID)
{
	uint8_t index = findRCRbyLinkID(RCRarray, numRCRs, linkID);
	RCRarray[index] -> ventStatus ^= 1;					// toggle vent status, for debugging
	ackMessage[1] = RCRarray[index] -> sleepTime;
	ackMessage[2] = RCRarray[index] -> ventStatus;
	SMPL_Send(linkID, ackMessage, ACK_MESSAGE_LENGTH);
}

void ackRTS(RTS_t** RTSarray, uint8_t numRTSs, linkID_t linkID)
{
	uint8_t index = findRTSbyLinkID(RTSarray, numRTSs, linkID);
	ackMessage[1] = RTSarray[index] -> sleepTime;
	SMPL_Send(linkID, ackMessage, ACK_MESSAGE_LENGTH);
}
